package com.entidades;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutionException;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import com.Adapter.AdapterType;
import com.Adapter.CustomTeamAdapter;
import com.CustomClasses.FetchMySQL;
import com.CustomClasses.FetchXML;
import com.bolao.MainTabActivity;
import com.bolao.R;

public class BetRow{
	

	private static final String BET_NAME = "bet_name";
	private static final String BET_ANSW = "bet_answ";
	private SpinnerAdapter adapter;
	private String text;
	private int ID;
	private AdapterType adapterType;
	private static HashMap<AdapterType, SpinnerAdapter> adapters;
	private int userBet;
	
	private static final String tagName = "bet";
	
	private static final  String[] fieldNames = new String[]{"id", "textString", "adapter"};
	
	private static final int LIST_ID = 0;
	
	private static final int LIST_TEXT = 1;
	
	private static final int LIST_ADAPTER = 2;
	
	//private static final String xmlFileName = "BetsList.xml";
	
	private static final String URL = "http://www.worldcupcompendium.com/entities/BetsList.xml";
	private static final String TABLE_NAME = "gen_answ";
	
	private static List<BetRow> betRows;
	private static List<BetRow> betTeamRows;
	private static List<BetRow> betGenRows;
	
	public String getColumnName() {
		return text;
	}
	
	public int getID() {
		return ID;
	}

	
	public int getUserBet() {
		return userBet;
	}


	public void setUserBet(int userBet) {
		this.userBet = userBet;
	}
	
	public static List<HashMap<String, String>> getAllUserBet(List<BetRow> list){

		List<HashMap<String, String>> gList = new ArrayList<HashMap<String,String>>();
		for(BetRow bet: list){
			if(bet.getUserBet()!=-1){
			HashMap<String, String> hm = new HashMap<String, String>();
			
			hm.put(BET_NAME, bet.getColumnName());
			hm.put(BET_ANSW, String.valueOf(bet.getUserBet()));
			gList.add(hm);
			}
		}
		return gList;
		
	}
	

	private BetRow(int id, String text, String adapter, int userBet){
		this.ID = id;
		this.text = text;
		adapterType = AdapterType.fromXMLString(adapter);
		this.adapter =adapters.get(adapterType);
		this.userBet = userBet;
	}


	public SpinnerAdapter getAdapter() {
		return adapter;
	}
	
	public AdapterType getAdaptersType(){
		return adapterType;
	}
	public String getText() {
		return text;
	}
	
	public static List<String> getAllColumnNames(){
		List<String> rl = new ArrayList<String>();
			rl.add(BET_NAME);
			rl.add(BET_ANSW);
		return rl;
	}

	private static HashMap<AdapterType, SpinnerAdapter> getAdapters(){
		if(adapters==null){
			adapters = new HashMap<AdapterType, SpinnerAdapter>();
		CustomTeamAdapter teamAdapter;
		teamAdapter = new CustomTeamAdapter(MainTabActivity.getmContext() , Team.getTeams());
		adapters.put(AdapterType.TEAM_ADAPTER, teamAdapter);
		//ArrayAdapter<CharSequence> goalScoreAdapter = ArrayAdapter.createFromResource(mContext, R.array.goalScore, android.R.layout.simple_spinner_item);
		SimpleTextAdapter goalDiffAdapter = new SimpleTextAdapter(MainTabActivity.getmContext(), android.R.layout.simple_spinner_item, MainTabActivity.getmContext().getResources().getStringArray(R.array.goalDiff));
		SimpleTextAdapter goalScoreAdapter = new SimpleTextAdapter(MainTabActivity.getmContext(), android.R.layout.simple_spinner_item, MainTabActivity.getmContext().getResources().getStringArray(R.array.goalScore));
		
	//	ArrayAdapter<CharSequence> goalDiffAdapter = ArrayAdapter.createFromResource(mContext, R.array.goalDiff, android.R.layout.simple_spinner_item);
		adapters.put(AdapterType.GOAL_SCORE, goalScoreAdapter);
		adapters.put(AdapterType.GOAL_DIFF_ADAPTER, goalDiffAdapter);

		}
	return adapters;
	}
	
	public static List<BetRow> getBetRows () {
		if(betRows==null){
			BetRow.adapters = getAdapters();
		try {
					
		List<List<String>> list = new FetchXML().execute(URL, tagName, fieldNames).get();
		List<HashMap<String, String>> listUa = new FetchMySQL(TABLE_NAME, getList(), MainTabActivity.getUser().getId()).execute().get();
		betRows = new ArrayList<BetRow>();
		for(List<String> l: list){
			String betName = l.get(LIST_TEXT);
			int userBet = -1;
			for(HashMap<String, String> map: listUa){
				if(map.get(BET_NAME).equals(String.valueOf(betName))){
					userBet = Integer.valueOf(map.get(BET_ANSW));
				}
			}		
			betRows.add(new BetRow(Integer.valueOf(l.get(LIST_ID)), betName, l.get(LIST_ADAPTER), userBet));
			}	
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		return betRows;
		
	}
	
	private static List<String> getList() {
		List<String> ls = new ArrayList<String>();
		ls.add(BET_ANSW);
		ls.add(BET_NAME);
		return ls;
	}

	public static List<BetRow> getTeamBets(){
		if(betTeamRows==null){
			betTeamRows = new ArrayList<BetRow>();
		for(BetRow br:getBetRows()){
			if(br.getAdaptersType().equals(AdapterType.TEAM_ADAPTER)){
				betTeamRows.add(br);
			}
		}
		}
		return betTeamRows;
	}
	
	public static List<BetRow> getGenBets(){
		if(betGenRows==null){
			betGenRows = new ArrayList<BetRow>();
		for(BetRow br:getBetRows()){
			if(br.getAdaptersType().equals(AdapterType.GOAL_DIFF_ADAPTER)){
				betGenRows.add(br);
			}
		}
		}
		return betGenRows;
	}

	static class SimpleTextAdapter extends ArrayAdapter<String> {

		private Context context;

		public SimpleTextAdapter(Context context, int resource, String[] objects) {
				super(context, resource, objects);
				this.context = context;
			}

			public View getView(int position, View convertView, ViewGroup parent) {
		        View v = super.getView(position, convertView, parent);
		        ((TextView) v).setTextSize(16);
		        ((TextView) v).setTextColor(context.getResources().getColorStateList(android.R.color.black));
		        return v;
		    }
		}

	
}